﻿//
// Created by work on 24-6-5.
//

#ifndef IOT_PLAT_CMODELINFO_H
#define IOT_PLAT_CMODELINFO_H

#include <shlib/CValue.h>
#include <shlib/viewmodel/imodel.h>
#include <QObject>

class CModelInfo : public IModel{
    Q_OBJECT
public:
    enum EModelCol {
        EName = 0x00,
        EUser,
        EDescr,
        ESn,
        EGroupIngs,
        ELocation,
        ECreate_time,
        EUpdate_time,
        EProducer,
        EValid,
        EConfigs
    };

    QString name;
    QString user;
    QString descr;
    int sn;
    QString grouping;
    QString location;
    QDateTime create_time;
    QDateTime update_time;
    QString producer;
    bool valid;
    QJsonObject configs;

    static std::map<CModelInfo::EModelCol, QString> rtModelColToStringMap;
    static std::map<CModelInfo::EModelCol, QString> modelColFieldsNameMap;
    static std::map<CModelInfo::EModelCol, CValue::EValueType> rtModelDataTypeByField;

public:
    CValue getValueByField(CModelInfo::EModelCol field);
    explicit  CModelInfo(QObject *parent = nullptr);

public:



public:
    virtual QString getFieldNameByCol(int col)override;

    virtual QVariant getFieldValueByFielddName(const QString& name)override;

    virtual void setFieldValueByCol(int col, const QVariant& value)override;

    virtual QJsonObject toJson()override;

    virtual void fromJson(const QJsonObject &json)override;

    virtual QSharedPointer<IModel> createEmpty()override;

    virtual bool sync(bool dir = true) override;

protected:

};

#define MODEL_ENUM_NAME             CModelInfo::EModelCol::EName
#define MODEL_ENUM_USER             CModelInfo::EModelCol::EUser
#define MODEL_ENUM_DESCR            CModelInfo::EModelCol::EDescr
#define MODEL_ENUM_SN               CModelInfo::EModelCol::ESn
#define MODEL_ENUM_GROUPINGS        CModelInfo::EModelCol::EGroupIngs
#define MODEL_ENUM_LOCATION         CModelInfo::EModelCol::ELocation
#define MODEL_ENUM_CREATE_TIME      CModelInfo::EModelCol::ECreate_time
#define MODEL_ENUM_UPDATE_TIME      CModelInfo::EModelCol::EUpdate_time
#define MODEL_ENUM_PRODUCER         CModelInfo::EModelCol::EProducer
#define MODEL_ENUM_VALID            CModelInfo::EModelCol::EValid
#define MODEL_ENUM_CONFIGS          CModelInfo::EModelCol::EConfigs
#define MODEL_ENUM                  CModelInfo::EModelCol

#define mdoelFieldName(type)        CModelInfo::rtModelColToStringMap.find(type)->second
#define modelColFieldName(type)     CModelInfo::modelColFieldsNameMap.find(type)->second
#define mdoelFieldType(type)        CModelInfo::rtModelDataTypeByField.find(type)->second
#define MODEL_COL_SIZE              CModelInfo::rtModelColToStringMap.size()


Q_DECLARE_METATYPE(QSharedPointer<CModelInfo>)

#endif
